﻿#region  Copyright (C) 2013 Unisys Corporation

// =============================================================================================
// All Rights Reserved. This work contains trade secrets and confidential material of UNISYS, 
// and its use or disclosure in whole or in part without explicit written permission of 
// UNISYS is prohibited. 
//  
// Product Name          :  Unisys e-Workflow and Imaging 8.2
// File Name             :  Administrator.cs
// Authored By           :  Unisys Corporation
// =============================================================================================
//	Modified by                         Date            Version
// =============================================================================================
//	ronald.aguilar@cr.unisys.com      Jun 06, 2013    Initial Version
// =============================================================================================

#endregion

#region Using Region
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Unisys.DuplicateCheck.Logic.Services;
using Unisys.DuplicateCheck.Logic.BDAdministrator;
using Unisys.DuplicateCheck.Entity.Model;
using System.Data.SqlClient;
#endregion

namespace Unisys.DuplicateCheck.Logic
{
    public class Administrator
    {
        public void ProcessManager()
        {
            DuplicateService businessRules;
            #region Load Business Rules
            try
            {
                businessRules = DuplicateService.loadBusinessRules();
                if (businessRules == null)
                {
                    Service.sendLog("Error al cargar las reglas de negocio.", Service.errorType.Error_Message);
                    return;

                }
            }
            catch (Exception exc) 
            {
                Service.sendLog("Error al cargar las reglas de negocio: " + exc.Message, Service.errorType.Error_Message);
                return;
            }

            #endregion

            #region Querys Generation
            #endregion

            BDManager dataBase = new BDManager();
            SqlDataReader queueReader = null;


            #region Read List
            try
            {
               

                foreach (DocClass docClass in businessRules.DocClassList)
                {
                    queueReader = dataBase.DuplicateList(businessRules, docClass);
                    if (queueReader == null)
                    {
                        Service.sendLog("Imposible ejecutar query a la Base de Datos de InfoImage: Reader = Null", Service.errorType.Error_Message);
                        return;
                    }

                    #region Process Duplicate Documents (Database Rows)
                    if (queueReader.HasRows)
                    {
                        Service.sendLog("Procesando Documentos...", Service.errorType.Information_Message);
                        List<duplicateDocument> duplicateDocuments = dataBase.convertReaderToList(queueReader);
                        queueReader.Close();


                        dataBase.processDocument(duplicateDocuments);
                        Service.sendLog("Proceso finalizado...", Service.errorType.Information_Message);
                    }

                    #endregion

                }
               



            }
            catch (Exception sqlExc)
            {
                Service.sendLog("Imposible ejecutar query a la Base de Datos de InfoImage: " + sqlExc.Message, Service.errorType.Error_Message, sqlExc);
                Service.sendLog("Se detiene el proceso de validacion de duplicados", Service.errorType.Log_Message, null, "Fin del Proceso");

                return;
            }
            #endregion


        }

       

    }
}
